package org.honeyweb.gsoc.dao.hibernate3;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.honeyweb.gsoc.dao.UsersDao;
import org.honeyweb.gsoc.model.Users;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao{


	

	/**
	 * {@inheritDoc}
	 */
	public boolean checkLogin(String login, String password) {
		if (null == login || null == password) {
			throw new IllegalArgumentException("Login and password are mandatory. Null values are forbidden.");
		}		
		try {
			logger.info("Check user with login: "+login+" and password : [PROTECTED]");
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			// create a new criteria
			Criteria crit = session.createCriteria(Users.class);
			crit.add(Restrictions.ilike("login", login));
			crit.add(Restrictions.eq("password", password));
			logger.info(crit);

			
			Users user = (Users)crit.uniqueResult();
			return (user != null);
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete checkLogin().");
			return false;
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Users getUser(String login) {
		if (null == login) {
			throw new IllegalArgumentException("Login is mandatory. Null value is forbidden.");
		}
		try {
			logger.info("get User with login: "+login);
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			// create a new criteria
			Criteria crit = session.createCriteria(Users.class);
			crit.add(Restrictions.eq("login", login));
			
			Users user = (Users)crit.uniqueResult();
			return user;
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete getUser().");
			return null;
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addUser(Users user){
		if (null == user) {
			throw new IllegalArgumentException("A user has to be specified");
		}	
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.save(user);
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete checkLogin().");
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void updateUserOrganizationsId(int organizationsId, int id){
		if (0 == organizationsId) {
			throw new IllegalArgumentException("A user has to be specified");
		}	
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			Users user = (Users) session.load(Users.class, new Integer(id)); 
			user.setOrganizationsId(organizationsId); 
			session.update(user);
			session.flush();
			session.refresh(user);
			
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete checkLogin().");
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
    public Users getAdmin(int adminId){
		if (0 == adminId) {
			throw new IllegalArgumentException("adminId is mandatory. Null value is forbidden.");
		}
		try {
			logger.info("get User with AdminId: "+adminId);
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			// create a new criteria
			Criteria crit = session.createCriteria(Users.class);
			crit.add(Restrictions.eq("id", adminId));
			
			Users user = (Users)crit.uniqueResult();
			return user;
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete getUser().");
			return null;
		}
    }
    
	/**
	 * {@inheritDoc}
	 */
	public void updateUser(String login, String firstName, String lastName, String mail,int id){
		if (null == login || null == firstName || null == lastName || null == mail) {
			throw new IllegalArgumentException("NULL values forbidden");
		}	
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			Users user = (Users) session.load(Users.class, new Integer(id)); 
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setLogin(login);
			user.setMail(mail);
			session.update(user);
			session.flush();
			session.refresh(user);
			
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete checkLogin().");
		}
	}
    
    
}
